import {observable, action, runInAction} from 'mobx';

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class CarOrderStore {
    @observable plateNumber; //车牌
    @observable payments = 0;//付款金额
    @observable parkingTime = 0; //停车时长
    @observable isLoading = true;
    @observable detail;
    @observable attach;
    @observable attach2;
    isPaySubmit = false;

    @action
    getOrderInfo = (props) => {
        this.code = getUrlParam("code");
        this.laneId = getUrlParam("laneId");
        let params = {};
        params.laneId = this.laneId;
        postJson("/api/v2/car/anon/queryCarLaneOrder", params, (data) => {
            data = JSON.parse(data);
            console.log("data: ", data);
            runInAction(() => {
                this.parkId = this.attach2 = data.parkId;
                this.laneCode = data.laneCode;
                this.plateNumber = this.attach = data.plateNumber;
                this.payments = data.payments;
                this.parkingTime = data.period;
                this.isLoading = false;
                this.detail = JSON.stringify({
                    laneCode: this.laneCode
                });
            });
        }, (error) => {
            console.log("error: ", error);
            props.history.push({
                pathname: '/payAuth/jhcloud/payFail'
            }, {
                errorMsg: error.data
            });
        });
    };

    @action
    paySubmit = (props) => {
        if (!this.isPaySubmit) {
            this.isPaySubmit = true;
            let params = {
                payType: "wx_h5",
                body: '车辆临时缴费',
                orderType: "CAR_LANE",
                totalFee: 0.01,
                payableFee: this.payments,
                code: this.code,
                attach: this.plateNumber,
                attach2: this.parkId,
                detail: this.detail
            };
            postJson("/api/v2/pay/anon/weixin/H5/order", params, (data) => {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', data,
                    (res) => {
                        //使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                        if (res.err_msg === "get_brand_wcpay_request:ok") {
                            props.history.push({
                                pathname: '/payAuth/jhcloud/paySuccess',
                                orderInfo: {
                                    plateNumber: this.plateNumber,
                                    payments: this.payments,
                                    creationTime: this.creationTime
                                }
                            });
                        } else {
                            props.history.push({
                                pathname: '/payAuth/jhcloud/payFail'
                            });
                        }
                    }
                );
            }, (data) => {
                props.history.push({
                    pathname: '/payAuth/jhcloud/payFail'
                }, {
                    errorMsg: data.data
                });
            })
        }
    };
}

export default new CarOrderStore()